<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * @param Author: Tejo Murti
 * This helper can upload file.
 * please set the model to determine the type of files allowed
 * 
 * $field_name : the name of your input type
 * $path: your path, where the file is located
 * $model: your model
 * 
 */

class UploadFileHelper {
   
    public static function upload_file($field_name, $path, $model)
    {
        //$photo_new_name=null;
        $photo_new_name=$old_name_photo=$model->$field_name;
                         
        $file = CUploadedFile::getInstance($model, $field_name);
                        
        if((is_object($file) && get_class($file) === 'CUploadedFile'))
        {
            $folder_module=Yii::app()->basePath.'/..'.$path;
           
            if(!is_dir($folder_module))
            {
                mkdir($folder_module);
                chmod($folder_module, 0777);
            }
                        
            $model->$field_name= $file;
            
            $endStr=$model->$field_name->extensionName; 
            
            if(file_exists($folder_module.$file))
            {
                $photo_new_name=FileHelper::generateRandomName(rand(1,9), str_replace($endStr, '', $file).$endStr);
            }
            else
            {
                 $photo_new_name=$file;
            }
            
            $model->$field_name=$photo_new_name;
            $model->$field_name= CUploadedFile::getInstance($model, $field_name);
           
            //SIMPAN GAMBAR KE FOLDER
            $model->$field_name->saveAs($folder_module . $photo_new_name);
                    
            if(isset($old_name_photo) AND $old_name_photo!='')
            {
                if (file_exists($folder_module. $old_name_photo) AND $old_name_photo!='') 
                    {
                        unlink($folder_module . $old_name_photo);
                    }
            }     
           
        }
                          
          return $photo_new_name;              

    }
}
?>
